package com.goldsprite.microhorigame.ecs;

import com.goldsprite.gameframeworks.ecs.component.*;
import com.goldsprite.gameframeworks.fsm.Fsm;

public class EntityFsmComponent extends Fsm implements IComponent {
	public final ComponentField fields = new ComponentField();

	protected EntityInputComponent inputs;
	protected AnimatorComponent anims;
	protected EntityPropsComponent props;
	protected RigidbodyComponent rigi;
	private RectColliderComponent bodyCollider;
	private ColliderComponent attackTrigger;
	private FootTriggerComponent footTrigger;
	private Runnable initializeStates;

	private float hurtDamage;

	public EntityFsmComponent() {
		initIComponent();
	}

	public void onInitializeStates(Runnable callback) {
		initializeStates = callback;
	}

	@Override
	public void awake() {
		IComponent.super.awake();
		inputs = getComponent(EntityInputComponent.class);
		anims = getComponent(AnimatorComponent.class);
		props = getComponent(EntityPropsComponent.class);
		rigi = getComponent(RigidbodyComponent.class);
		//初始化状态组
		if(initializeStates != null) initializeStates.run();
	}

	public TransformComponent getTrans() {
		return fields.transform;
	}

	public EntityInputComponent getInputs() {
		return inputs;
	}

	public AnimatorComponent getAnims() {
		return anims;
	}

	public EntityPropsComponent getProps() {
		return props;
	}

	public RigidbodyComponent getRigi() {
		return rigi;
	}

	public void setAttackTrigger(ColliderComponent attackTrigger) {
		this.attackTrigger = attackTrigger;
	}
	public ColliderComponent getAttackTrigger(){
		return attackTrigger;
	}

	public void setFootTrigger(FootTriggerComponent footTrigger) {
		this.footTrigger = footTrigger;
	}
	public FootTriggerComponent getFootTrigger() {
		return footTrigger;
	}

	public boolean isGround() {
		return footTrigger.isGround();
	}

	@Override
	public void update(float delta) {
		updateFsm();
	}

	public void trigHurt(float damage) {
		getInputs().setKeyHurt(true);
		hurtDamage = damage;
	}
	public float getHurtDamage() {
		return hurtDamage;
	}

	@Override
	public ComponentField getCompFields() {
		return fields;
	}

	public boolean isFalling() {
		return rigi.getVelocity().y < -0f;
	}

	public void setBodyCollider(RectColliderComponent bodyCollider) {
		this.bodyCollider = bodyCollider;
	}
	public RectColliderComponent getBodyCollider() {
		return bodyCollider;
	}
}
